package day07;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author zouran
 * createDate：2024/6/28  16:53
 */
public class Client {
    public static void main(String[] args) throws IOException, InterruptedException {
        String host = "127.0.0.1";
        int port = 10000;
        Socket socket = new Socket(host, port);
        System.out.println("Client Start！");
        char[] chars; //发送的最大消息不超过1024*1024 个字符
        String message = "Nice to meet you!";
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        while (true) {
            chars = new char[1024];
            Scanner scanner = new Scanner(System.in);
            System.out.print("Please input message:");
            message = scanner.nextLine();
            bufferedWriter.write(message);
            bufferedWriter.flush();
            System.out.println("Send Success!");
            int len = bufferedReader.read(chars);
            System.out.print("Receive Server Message: ");
            System.out.println(new String(chars, 0, len));
        }

    }
}
